package net.seabook.games.tetris.model;

import java.awt.Color;

import net.seabook.games.tetris.model.Tile.TileColor;
import net.seabook.games.tetris.ui.PreviewPanel;

public class PieceGenerator {
	private TetrisBoard board;
	private Piece piece;
	private Piece previewPiece;
	
	private static final int FULL_START_X = 4 * Tile.WIDTH;
	private static final int FULL_START_Y = 0;
	private static final int I_START_X = 3 * Tile.WIDTH;
	private static final int I_START_Y = 0;
	private static final int LL_START_X = 4 * Tile.WIDTH;
	private static final int LL_START_Y = 2 * Tile.HEIGHT;
	private static final int RL_START_X = 6 * Tile.WIDTH;
	private static final int RL_START_Y = 2 * Tile.HEIGHT;
	private static final int LZ_START_X = 4 * Tile.WIDTH;
	private static final int LZ_START_Y = 0;
	private static final int RZ_START_X = 4 * Tile.WIDTH;
	private static final int RZ_START_Y = Tile.HEIGHT;
	private static final int T_START_X = 4 * Tile.WIDTH;
	private static final int T_START_Y = Tile.HEIGHT;
	
	private static final int PREVIEW_FULL_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + 3 * Tile.WIDTH / 2;
	private static final int PREVIEW_FULL_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 3 * Tile.HEIGHT / 2;
	private static final int PREVIEW_I_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + Tile.WIDTH / 2;
	private static final int PREVIEW_I_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 2 * Tile.HEIGHT;
	private static final int PREVIEW_LL_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + Tile.WIDTH;
	private static final int PREVIEW_LL_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 3 * Tile.HEIGHT;
	private static final int PREVIEW_RL_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + 3 * Tile.WIDTH;
	private static final int PREVIEW_RL_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 3 * Tile.HEIGHT;
	private static final int PREVIEW_LZ_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + Tile.WIDTH;
	private static final int PREVIEW_LZ_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 2 * Tile.HEIGHT;
	private static final int PREVIEW_RZ_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + Tile.WIDTH;
	private static final int PREVIEW_RZ_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 3 * Tile.HEIGHT;
	private static final int PREVIEW_T_START_X = PreviewPanel.PREVIEW_BLOCK_ORG_X + Tile.WIDTH;
	private static final int PREVIEW_T_START_Y = PreviewPanel.PREVIEW_BLOCK_ORG_Y + 3 * Tile.HEIGHT;
	
	public void generateRandomPiece() {
		Color ranColor = TileColor.getRandomTileColor();
		PieceEnum pieceRandom =  PieceEnum.randomPickUp();
		switch (pieceRandom) {
		case Full:
			piece = new FullBlock(FULL_START_X, FULL_START_Y);
			break;
		case I:
			piece = new IBlock(I_START_X, I_START_Y);
			break;
		case LL:
			piece = new LLBlock(LL_START_X, LL_START_Y);
			break;
		case RL:
			piece = new RLBlock(RL_START_X, RL_START_Y);
			break;
		case LZ:
			piece = new LZBlock(LZ_START_X, LZ_START_Y);
			break;
		case RZ:
			piece = new RZBlock(RZ_START_X, RZ_START_Y);
			break;
		case T:
			piece = new TBlock(T_START_X, T_START_Y);
			break;
		}
		if (piece != null) {
			piece.setPieceColor(ranColor);
			piece.setBoard(board);
		}
		
	}
	
	private Piece generateRanPreviewPiece() {
		Color ranColor = TileColor.getRandomTileColor();
		PieceEnum pieceRandom =  PieceEnum.randomPickUp();
		Piece piece = null;
		switch (pieceRandom) {
		case Full:
			piece = new FullBlock(PREVIEW_FULL_START_X, PREVIEW_FULL_START_Y);
			break;
		case I:
			piece = new IBlock(PREVIEW_I_START_X, PREVIEW_I_START_Y);
			break;
		case LL:
			piece = new LLBlock(PREVIEW_LL_START_X, PREVIEW_LL_START_Y);
			break;
		case RL:
			piece = new RLBlock(PREVIEW_RL_START_X, PREVIEW_RL_START_Y);
			break;
		case LZ:
			piece = new LZBlock(PREVIEW_LZ_START_X, PREVIEW_LZ_START_Y);
			break;
		case RZ:
			piece = new RZBlock(PREVIEW_RZ_START_X, PREVIEW_RZ_START_Y);
			break;
		case T:
			piece = new TBlock(PREVIEW_T_START_X, PREVIEW_T_START_Y);
			break;
		}
		if (piece != null) {
			piece.setPieceColor(ranColor);
			piece.setBoard(board);
		}
		return piece;
	}
	
	public void generatePreviewPiece() {
		previewPiece = generateRanPreviewPiece();
	}
	
	public void shiftPiece() {
		if (previewPiece instanceof FullBlock) {
			piece = new FullBlock(FULL_START_X, FULL_START_Y);
		}  else if (previewPiece instanceof IBlock) {
			piece = new IBlock(I_START_X, I_START_Y);
		} else if (previewPiece instanceof LLBlock) {
			piece = new LLBlock(LL_START_X, LL_START_Y);
		} else if (previewPiece instanceof RLBlock) {
			piece = new RLBlock(RL_START_X, RL_START_Y);
		} else if (previewPiece instanceof LZBlock) {
			piece = new LZBlock(LZ_START_X, LZ_START_Y);
		} else if (previewPiece instanceof RZBlock) {
			piece = new RZBlock(RZ_START_X, RZ_START_Y);
		} else if (previewPiece instanceof TBlock) {
			piece = new TBlock(T_START_X, T_START_Y);
		}
		piece.setBoard(board);
		for (Tile t1 : piece.getBlocks()) {
			for (Tile t2 : previewPiece.getBlocks()) {
				t1.setFilledColor(t2.getFilledColor());
			}
		}
		generatePreviewPiece();
	}

	public Piece getPiece() {
		return piece;
	}

	public void setPiece(Piece piece) {
		this.piece = piece;
	}

	public Piece getPreviewPiece() {
		return previewPiece;
	}

	public void setPreviewPiece(Piece next) {
		this.previewPiece = next;
	}

	public TetrisBoard getBoard() {
		return board;
	}

	public void setBoard(TetrisBoard board) {
		this.board = board;
	}
}
